﻿// ------------------------------------------------------------------------------------
//      Copyright (c) uhavemyword@gmail.com. All rights reserved.
//      Created by Ben at 2/7/2013 10:38:25 AM
// ------------------------------------------------------------------------------------

namespace CP.NLayer.WPFClient.Common
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Threading;

    public class RadBusyIndicator : ViewModelBase
    {
        private bool _isBusy;
        private bool _isIndeterminate;
        private double _progressValue;
        private string _busyContent;
        private TimeSpan _displayAfter;
        private DispatcherTimer _progressValueTimer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(40) };
        
        public TimeSpan DisplayAfter
        {
            get
            {
                return this._displayAfter;
            }
            set
            {
                if (this._displayAfter != value)
                {
                    this._displayAfter = value;
                    this.RaisePropertyChanged(() => this.DisplayAfter);
                }
            }
        }

        public bool IsIndeterminate
        {
            get
            {
                return this._isIndeterminate;
            }
            set
            {
                if (this._isIndeterminate != value)
                {
                    this._isIndeterminate = value;
                    this.RaisePropertyChanged(() => this.IsIndeterminate);
                    this.RaisePropertyChanged(() => this.BusyContent);
                }
            }
        }

        public double ProgressValue
        {
            get
            {
                return this._progressValue;
            }
            set
            {
                if (this._progressValue != value)
                {
                    this._progressValue = value;
                    this.RaisePropertyChanged(() => this.ProgressValue);
                    this.RaisePropertyChanged(() => this.BusyContent);
                }
            }
        }

        public bool IsBusy
        {
            get
            {
                return this._isBusy;
            }
            set
            {
                if (this._isBusy != value)
                {
                    this._isBusy = value;
                    this.RaisePropertyChanged(() => this.IsBusy);

                    if (this._isBusy)
                    {
                        this._progressValueTimer.Tick += this.OnProgressValueTimerTick;
                        this._progressValueTimer.Start();
                        this.ProgressValue = 0;
                    }
                    else
                    {
                        this._progressValueTimer.Tick -= this.OnProgressValueTimerTick;
                        this._progressValueTimer.Stop();
                    }
                }
            }
        }

        public string BusyContent
        {
            get 
            {
                return this.IsIndeterminate ? "Loading..." : string.Concat(this.ProgressValue, "% loaded"); 
            }
            set
            {

            }
        }

        private void OnProgressValueTimerTick(object sender, EventArgs e)
        {
            if (this.IsIndeterminate)
            {
                return;
            }

            if (this.ProgressValue < 100)
            {
                this.ProgressValue++;
            }
            else
            {
                this.IsBusy = false;
            }
        }
    }
}
